package com.sts.transform.impl;

import cn.hutool.core.util.StrUtil;
import com.googlecode.aviator.AviatorEvaluator;
import com.sts.transform.StsTransform;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * @author lsl
 * @since 2025年07月08日 19:39
 */
public class RuleGenerateTransformImpl implements StsTransform<List<String>> {

    private final String mock;

    public RuleGenerateTransformImpl(String mock) {
        this.mock = mock;
    }

    @Override
    public Stream<List<String>> transform(Stream<List<String>> dataStream) {
        return dataStream.map(this);
    }

    @Override
    public List<String> apply(List<String> row) {
        List<String> templist = new ArrayList<>(row);
        for (int i = 0; i < templist.size(); i++) {
            String value = templist.get(i);
            if (StrUtil.startWith(value, mock)) {
                value = StrUtil.subAfter(value, mock, false);
                Object executeValue = AviatorEvaluator.execute(value, Map.of("row", templist, "mock", mock), true);

                if (executeValue != null) {
                    templist.set(i, String.valueOf(executeValue));
                }
            }
        }
        return templist;
    }
}
